package com.example.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DataSourceUtil {

    private static final Logger log = Logger.getLogger(DataSourceUtil.class.getCanonicalName());

    private static final String driver = "com.mysql.cj.jdbc.Driver";

    private static final String url = "jdbc:mysql:// 172.19.165.160:3306/snowy-solon";

    private static final String username = "solon";

    private static final String password = "solon";


    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            log.log(Level.SEVERE,"JDBC驱动加载失败！",e);
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection(){
        Connection conn =  null;
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            log.log(Level.SEVERE,"获取数据库连接失败！",e);
            throw new RuntimeException(e);
        }
        return conn;
    }

    public static void releaseConnection(Connection conn){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                log.log(Level.SEVERE,"释放数据库连接失败！",e);
                throw new RuntimeException(e);
            }
        }
    }

}
